package document.action;



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import document.action.form.DocumentActionForm;
import document.service.DocCRUDService;
import document.vo.DocumentVO;

public class DAOAction extends DispatchAction {
	private DocCRUDService service;
	
	public void setService(DocCRUDService service){
		this.service = service;
	}
	
	public ActionForward registerDocument(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		DocumentActionForm docForm = (DocumentActionForm)form;
		DocumentVO dvo = service.insertDoc(docForm);
		request.setAttribute("dvo", dvo);
		return mapping.findForward("registerSuccess");
	}
	public ActionForward selectDocument(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		String result = "show_document";
		String documentId = request.getParameter("documentId");
		DocumentVO dvo = service.selectDoc(documentId);
		if(dvo==null){
			result="fail";
		}
		request.setAttribute("dvo", dvo);
		return mapping.findForward(result);
	}
	public ActionForward deleteDocument(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		String documentId = request.getParameter("documentId");
		String result = null;
		result = service.deleteDoc(documentId);
		return mapping.findForward(result);
	}
	public ActionForward updateDocument(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		DocumentActionForm docForm = (DocumentActionForm)form;
		String result = null;
		result = service.updateDoc(docForm);
		return mapping.findForward(result);
	}
	public ActionForward viewToModify(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		String documentId=request.getParameter("documentId");
		DocumentVO dvo = service.selectDoc(documentId);
		request.setAttribute("dvo",dvo);
		return mapping.findForward("toModify");
	}
	
	public ActionForward selectAllDoc(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
			request = service.selectAllDocService(request);
		return mapping.findForward("docList");
	}
	public ActionForward selectDocByDeptId(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
			request = service.selectDocByDeptIdService(request);
		return mapping.findForward("listByDeptId");
	}
	
	public ActionForward selectDocByWriter(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
			request = service.selectDocByWriterService(request);
		return mapping.findForward("listByWriter");
	}
	public ActionForward selectDocByAprovalState(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		request = service.selectDocByAprovalInfoState(request);
		if(request.getAttribute("lvo")==null)
			return mapping.findForward("fail");
		return mapping.findForward("listByAprovalState");
	}
	public ActionForward setAproval(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		service.setAproval(request);
		return mapping.findForward("aprovalSuccess");
	}
	public ActionForward setAprovalRefuse(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		service.setAprovalRefuse(request);
		return mapping.findForward("aprovalSuccess");
	}
	
}
